package com.ycx.furns.web;

import com.ycx.furns.entity.Member;
import com.ycx.furns.service.MemberService;
import com.ycx.furns.service.impl.MemberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
    MemberService memberService = new MemberServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("registerServlet 被调用~");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        if(memberService.registerCheck(username)){
            System.out.println("用户名可用，可以注册");
            if(memberService.registerMember(new Member(null, username, password, email))){
                System.out.println("注册成功~");
                req.getRequestDispatcher("/views/member/register_ok.jsp").forward(req,resp);
            }else {
                req.getRequestDispatcher("/views/member/register_fail.jsp").forward(req,resp);
            }
        }else{
            System.out.println("用户名已存在");
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
        }
    }
}
